import { contextBridge, ipcRenderer } from "electron/renderer";
import { FirewallProfile, NetIf, RouteConf } from "./type/interfaces.ts";


const apiKey = "electron";

export type ElectronApi = typeof electronApi;

const electronApi = {
  showDevTool() {
    return ipcRenderer.invoke("showDevTool");
  },
  openWorkspace() {
    return ipcRenderer.invoke("openWorkspace");
  },
  showRouteTable(): Promise<RouteConf[]> {
    return ipcRenderer.invoke("showRouteTable");
  },
  deleteRoute(data: RouteConf): Promise<void> {
    return ipcRenderer.invoke("deleteRoute", data);
  },
  changeRoute(type: string, data: RouteConf): Promise<void> {
    return ipcRenderer.invoke("changeRoute", type, data);
  },
  getNetworkInterfaces(): Promise<NetIf[]> {
    return ipcRenderer.invoke("getNetworkInterfaces");
  },
  getFirewallStatus(): Promise<FirewallProfile[]> {
    return ipcRenderer.invoke("getFirewallStatus");
  },
  setFirewallStatus(status: boolean, profile?: '域' | '专用' | '公用'): Promise<boolean> {
    return ipcRenderer.invoke("setFirewallStatus", status, profile);
  },
  restartNetworkAdapter(name: string): Promise<boolean> {
    return ipcRenderer.invoke("restartNetworkAdapter", name);
  },
  setNetworkAdapterStatus(name: string, enable: boolean): Promise<{ success: boolean; message: string }> {
    return ipcRenderer.invoke("setNetworkAdapterStatus", name, enable);
  },
  startHttpService(port: number): Promise<void> {
    return ipcRenderer.invoke("startHttpService", port);
  },
  stopHttpService(): Promise<void> {
    return ipcRenderer.invoke("stopHttpService");
  },
  startBroadCast(id: string, port: number): Promise<void> {
    return ipcRenderer.invoke("startBroadCast", id, port);
  },
  stopBroadCast(): Promise<void> {
    return ipcRenderer.invoke("stopBroadCast");
  },
  beginClient(port: number): Promise<void> {
    return ipcRenderer.invoke("beginClient", port);
  },
  stopClient(): Promise<void> {
    return ipcRenderer.invoke("stopClient");
  },
  addIpcListener(channel: string, listener: any): void {
    ipcRenderer.on(channel, listener);
  },
  delIpcListener(channel: string, listener: any): void {
    ipcRenderer.removeListener(channel, listener);
  },
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
